package com.scheduling.schedule.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.scheduling.schedule.entity.ProductionTask;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 生产任务Mapper接口
 *
 * @author 开发团队
 */
@Mapper
public interface ProductionTaskMapper extends BaseMapper<ProductionTask> {

    /**
     * 根据工单ID查询生产任务
     *
     * @param workOrderId 工单ID
     * @return 生产任务列表
     */
    @Select("SELECT * FROM production_task WHERE work_order_id = #{workOrderId} ORDER BY create_time ASC")
    List<ProductionTask> selectByWorkOrderId(@Param("workOrderId") String workOrderId);

    /**
     * 根据状态查询生产任务
     *
     * @param status 状态
     * @return 生产任务列表
     */
    @Select("SELECT * FROM production_task WHERE status = #{status} ORDER BY create_time ASC")
    List<ProductionTask> selectByStatus(@Param("status") String status);

    /**
     * 根据机器ID查询生产任务
     *
     * @param machineId 机器ID
     * @return 生产任务列表
     */
    @Select("SELECT * FROM production_task WHERE machine_id = #{machineId} ORDER BY plan_start_time ASC")
    List<ProductionTask> selectByMachineId(@Param("machineId") String machineId);

    /**
     * 查询待分配的生产任务
     *
     * @return 待分配任务列表
     */
    @Select("SELECT * FROM production_task WHERE status = '待分配' ORDER BY create_time ASC")
    List<ProductionTask> selectPendingTasks();

    /**
     * 批量插入生产任务
     *
     * @param productionTasks 生产任务的集合
     * @return 成功插入的数目
     */
    @Insert("<script>" +
            "INSERT INTO production_task (task_id, work_order_id, operation_id, machine_id, quantity, standard_time, " +
            "planned_time, status, plan_start_time, plan_end_time, actual_start_time, actual_end_time, " +
            "predecessor_tasks, create_time, update_time) VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.taskId}, #{item.workOrderId}, #{item.operationId}, #{item.machineId}, #{item.quantity}, #{item" +
            ".standardTime}, #{item.plannedTime}), #{item.status}, #{item.planStartTime}, #{item.planEndTime}, #{item" +
            ".actualStartTime}, #{item.actualEndTime}, #{item.predecessorTasks}, #{item.createTime}, #{item" +
            ".updateTime}" +
            "</foreach>" +
            "</script>")
    int batchInsert(@Param("list") List<ProductionTask> productionTasks);
} 